<?php

namespace App\Events;

use App\Message;
use App\Parsedown;
use App\Utils;

class SendPrivilegeMail
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    private function auth_description($auth_level) {
        switch ($auth_level) {
            case 1:
                return "恭喜你解锁了讨论权限";
            case 2:
                return "权限等级2";
            case 3:
                return "权限等级3";
            case 4:
                return "权限等级4";
            case 5:
                return "权限等级5";
            case 6:
                return "权限等级6";
            case 7:
                return "权限等级7";
            default:
                return "迷之权限";
        }

    }
    /**
     * Handle the event.
     *
     * @param  SolveProblem $event
     * @return void
     */
    public function handle(GetPrivilege $event)
    {
        $user = $event->user;
        $auth_level = $event->auth_level;
        $permission = $user->permission;
        if ($permission == null) {
            $permission = 0;
        }
        for ($i = $permission + 1; $i <= $auth_level; $i++) {
            $content = $this->auth_description($i);
            $parsedown = new Parsedown();
            $content = $parsedown->text($content);
            $messenger = new Message();
            $messenger->sendMail(1, $user->id, $content);
        }
        $user->permission = $auth_level;
        $user->save();
    }
}
